//
//  BrandTableViewCell.m
//  UGoods
//
//  Created by dllo on 16/1/17.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "BrandTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "FindModel.h"
#import "BaseLabel.h"
#import "BaseView.h"
#import "ConstantsDefine.h"
@interface BrandTableViewCell ()

@property (nonatomic, retain) UIImageView *logoImageView; /**< 商标图片 */
@property (nonatomic, retain) BaseLabel *brandNameLabel; /**< 品牌名 */
@property (nonatomic, retain) BaseLabel *positionLabel; /**< 定位 */
@property (nonatomic, retain) UIImageView *brandImageView; /**< 品牌图片 */
@property (nonatomic, retain) UIImageView *buyerImageView; /**< 买手图片 */
@property (nonatomic, retain) BaseLabel *buyerLabel; /**< 买手名 */
@property (nonatomic, retain) BaseLabel *buyerBriefLabel; /**< 买手简介 */
@property (nonatomic, retain) UIView *lineView;
@property (nonatomic, retain) BaseLabel *comeFromeLabel;
@property (nonatomic, retain) BaseView *backView;

@end

@implementation BrandTableViewCell

- (void)dealloc
{
    [_logoImageView release];
    [_brandNameLabel release];
    [_positionLabel release];
    [_brandImageView release];
    [_buyerImageView release];
    [_buyerLabel release];
    [_buyerBriefLabel release];
    [_lineView release];
    [_comeFromeLabel release];
    [_backView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backView = [[BaseView alloc] init];
        [self.contentView addSubview:_backView];
        [_backView release];
        
        self.logoImageView = [[UIImageView alloc] init];
        _logoImageView.layer.cornerRadius = 20 * WIDTH;
        _logoImageView.layer.borderWidth = 0.5;
        _logoImageView.layer.borderColor = [UIColor blackColor].CGColor;
        _logoImageView.layer.masksToBounds = YES;
        [self.backView addSubview:_logoImageView];
        [_logoImageView release];
        
        self.brandNameLabel = [[BaseLabel alloc] init];
//        _brandNameLabel.backgroundColor = [UIColor redColor];
        _brandNameLabel.font = [UIFont systemFontOfSize:15 * WIDTH];
        _brandNameLabel.textAlignment = NSTextAlignmentLeft;
        [self.backView addSubview:_brandNameLabel];
        [_brandNameLabel release];
        
        self.positionLabel = [[BaseLabel alloc] init];
//        _positionLabel.backgroundColor = [UIColor redColor];
        _positionLabel.font = [UIFont systemFontOfSize:12 * WIDTH];
        _positionLabel.textAlignment = NSTextAlignmentLeft;
        _positionLabel.textColor = [UIColor grayColor];
        [self.backView addSubview:_positionLabel];
        [_positionLabel release];
        
        self.brandImageView = [[UIImageView alloc] init];
        [self.backView addSubview:_brandImageView];
        [_brandImageView release];
        
        self.lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor lightGrayColor];
        [self.backView addSubview:_lineView];
        [_lineView release];
        
        self.comeFromeLabel = [[BaseLabel alloc] init];
        _comeFromeLabel.backgroundColor = [UIColor whiteColor];
        _comeFromeLabel.text = @"来自";
        _comeFromeLabel.font = [UIFont systemFontOfSize:13 * WIDTH];
        [self.backView addSubview:_comeFromeLabel];
        [_comeFromeLabel release];
        
        self.buyerImageView = [[UIImageView alloc] init];
        _buyerImageView.layer.cornerRadius = 10 * WIDTH;
        _buyerImageView.layer.masksToBounds = YES;
        [self.backView addSubview:_buyerImageView];
        [_buyerImageView release];
        
        self.buyerLabel = [[BaseLabel alloc] init];
        _buyerLabel.backgroundColor = [UIColor whiteColor];
        _buyerLabel.font = [UIFont systemFontOfSize:13 * WIDTH];
        [self.backView addSubview:_buyerLabel];
        [_buyerLabel release];
        
        self.buyerBriefLabel = [[BaseLabel alloc] init];
//        _buyerBriefLabel.backgroundColor = [UIColor redColor];
        _buyerBriefLabel.font = [UIFont systemFontOfSize:12 * WIDTH];
        _buyerBriefLabel.numberOfLines = 2;
        _buyerBriefLabel.textColor = [UIColor grayColor];
        [self.backView addSubview:_buyerBriefLabel];
        [_buyerBriefLabel release];
        
    }
    return self;
}

// 计算buyerBriefLabel宽度
+ (CGFloat)heightWithText:(NSString *)text {
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(1000 * WIDTH, 20 * HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:13 * WIDTH]} context:nil];
    
    return rect.size.width + 10;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height - 10 * HEIGHT);
    self.logoImageView.frame = CGRectMake(10 * WIDTH, 10 * HEIGHT, 40 * WIDTH, 40 * HEIGHT);
    self.brandNameLabel.frame = CGRectMake(60 * WIDTH, 10 * HEIGHT, self.contentView.frame.size.width - 70 * WIDTH, 20 * HEIGHT);
    self.positionLabel.frame = CGRectMake(60 * WIDTH, 30 * HEIGHT, self.contentView.frame.size.width - 70 * WIDTH, 20 * HEIGHT);
    self.brandImageView.frame = CGRectMake(10 * WIDTH, 60 * HEIGHT, self.contentView.frame.size.width - 20 * WIDTH, 200 * HEIGHT);
    self.lineView.frame = CGRectMake(0, 280 * HEIGHT, self.contentView.frame.size.width, 1);
    
    self.comeFromeLabel.frame = CGRectMake(0, 0, 30 * WIDTH, 20 * HEIGHT);
    _comeFromeLabel.center = CGPointMake(140 * WIDTH, self.lineView.center.y);
    
    self.buyerImageView.frame = CGRectMake(0, 0, 20 * WIDTH, 20 * HEIGHT);
    _buyerImageView.center = CGPointMake(165 * WIDTH, self.lineView.center.y);
    
    CGFloat w = [[self class] heightWithText:self.buyerLabel.text];
    self.buyerLabel.frame = CGRectMake(0, 0, w * WIDTH, 20 * HEIGHT);
    _buyerLabel.center = CGPointMake(210 * WIDTH, self.lineView.center.y);
    
    self.buyerBriefLabel.frame = CGRectMake(10 * WIDTH, 295 * HEIGHT, self.contentView.frame.size.width - 20 * WIDTH, 40 * HEIGHT);
}

- (void)setModel:(FindModel *)model {
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    [_logoImageView sd_setImageWithURL:[NSURL URLWithString:model.app_show_logo] placeholderImage:[UIImage imageNamed:@"place2"]];
    _brandNameLabel.text = [NSString stringWithFormat:@"[%@] %@", model.site_url, model.brand_name];
    _positionLabel.text = model.brand_position;
    [_brandImageView sd_setImageWithURL:[NSURL URLWithString:model.brand_figure_image] placeholderImage:[UIImage imageNamed:@"place"]];
    [_buyerImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"place2"]];
    _buyerLabel.text = [NSString stringWithFormat:@"%@的推荐", model.uname];
    _buyerBriefLabel.text = model.brief;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
